package com.example.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ExecutionTimeAspect {
    // 对所有方法进行环绕增强，计算执行时间
    @Around("execution(* com.example.aop.OrderService.add(..)) || " +
            "execution(* com.example.aop.OrderService.delete(..)) || " +
            "execution(* com.example.aop.OrderService.find(..))")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        
        // 执行目标方法
        Object result = joinPoint.proceed();
        
        long endTime = System.currentTimeMillis();
        System.out.println("方法执行时间：" + (endTime - startTime) + "毫秒");
        
        return result;
    }
}
